package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Category;
import top.oylan.manager.entity.extend.CategoryExtend;
import top.oylan.manager.entity.TypeCategory;
import top.oylan.manager.entity.example.CategoryExample;
import top.oylan.manager.entity.example.TypeCategoryExample;
import top.oylan.manager.mapper.CategoryMapper;
import top.oylan.manager.mapper.TypeCategoryMapper;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.enums.Types;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-02-12
 */
@Service("categoryService")
public class CategorySericeImpl implements CategoryService {

    @Resource
    CategoryMapper mapper;

    @Resource
    TypeCategoryMapper typeCategoryMapper;

    /**
     *  分页查询后台的分类列表
     * @param pager
     * @return
     */
    @Override
    public List<CategoryExtend> selectCategoryByPage(Pager<CategoryExtend> pager) {

        CategoryExtend category = pager.getCondition();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        // 赋值查询数量的条件

        pager.setTotalRow((int)mapper.countByExample(categoryExample));
        return mapper.selectByPager(pager);

    }

    @Override
    public List<Category> selectByExample(Category category) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria createria = example.createCriteria();
        return mapper.selectByExample(example);
    }

    @Override
    public List<Category> selectByExample(CategoryExample example) {
        return mapper.selectByExample(example);
    }

    @Override
    public CategoryExtend selectCategoryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveCategory(Category category, int [] types) throws Exception {
        mapper.insertSelective(category);
        saveTypeCategory(category.getId(),types);
    }

    @Override
    public void updateCategory(Category category, int [] types) throws Exception {
       mapper.updateByPrimaryKeySelective(category);
       TypeCategoryExample typeCategoryExample = new TypeCategoryExample();
       TypeCategoryExample.Criteria criteria = typeCategoryExample.createCriteria();
       criteria.andCategoryIdEqualTo(category.getId());
       typeCategoryMapper.deleteByExample(typeCategoryExample);
       saveTypeCategory(category.getId(),types);
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TypeCategory> selectCategoryType(Integer id) {
        return typeCategoryMapper.selectByCategoryId(id);
    }

    @Override
    public List<Category> selectCategoriesForProduct(Types type) {

        return mapper.selectCategoryByType(type.getType());
    }

    @Override
    public List<Short> categoryTree(List<Category> list, List<Short> result, int cateId) {
        for (Category parent : list) {
            if (parent.getParentId() == cateId) {
                result.add(parent.getId().shortValue());
                categoryTree(list, result, parent.getId());
            }
        }
        return result;
    }

    private void saveTypeCategory(Integer id, int [] types) {
        for (int i=0;i<types.length;i++){
            TypeCategory item = new TypeCategory();
            item.setTypeId(types[i]);
            item.setCategoryId(id);
            typeCategoryMapper.insertSelective(item);
        }
    }

    // for-api
    @Override
    public List<Category> selectCategoryByType(int typeId) {
        return mapper.selectCategoryByType(typeId);
    }

}
